import Vue from "vue";
import Vuex from "vuex";
Vue.use(Vuex);


import * as api from "../api/api";
import {getUrlParams} from "../script/uitils";
const {nickName, userId} = getUrlParams();
import {BASE_URL} from "@/common/const";
const mySetState = (key, value) => {
	store.commit("setState", {key, value});
};

let backData = [];
const store = new Vuex.Store({
	state: {
		noticeData: [], //预告
		columnData: [], //栏目列表
		recordData: [], //录播视频列表
		playUrl: "", //播放地址
		total: 0,
		isFullScreen: false,
	},
	getters: {},
	mutations: {
		setState(state, {key, value}) {
			state[key] = value;
		},
		SetRecordId(state, params) {
			state.recordData = params;
		},
		playVideo(state, {id, isLiving = false}) {
			const t = Date.now() + Math.floor(Math.random() * 1000000);
			const url = isLiving
				? `http://yncytz.gensee.com/webcast/site/entry/join-${id}?nickName=${nickName}_${userId}_3_1` //直播
				: `${BASE_URL}/client/recordLiveOff.htm?t=${Date.now()}&recordId=${id}&nickName=${nickName}_${userId}_3_1`; //录播
			state.playUrl = url;
		},
	},
	actions: {
		//预告列表
		async getNoticeList({commit}, params) {
			const res = await api.getNoticeList(params);
			console.log("--res.noticeData--", res);
			const data = res.data.map(handlerNoticeData);
			mySetState("noticeData", data);
			const matchPlayId = window.location.href.match(/playId=(\w+)/);
			const id = matchPlayId ? matchPlayId[1] : "";
			//初始加载时, 如果有直播则立即播放
			if (params.play !== false) {
				if (id) return; //首页预告/录播跳转不循环直播
				const liveItem = res.data.filter((it) => it.status == 1);
				if (liveItem.length > 0) {
					const data = liveItem[0];
					window.reportParams = {
						id: data.channelId,
						cateId: data.configId,
						videoName: data.channelName,
						planTime: data.beginTime,
					};
					commit("playVideo", {id: data.channelId, isLiving: true});
				}
			}
			
			return data;
		},
		//录播栏目
		/*async queryVideoConfig(_, params) {
			const res = await api.queryVideoConfig(params);
			const all = [{channelName: "全部", id: ""}];
			mySetState("columnData", all.concat(res.data));
		},*/
		//录播视频列表
		/*async queryRecordliveList(_, params) {
			return api.queryRecordliveList(params).then((res) => {
				const back = res.data.videos.map((item) => {
					const start = item.recordStartTimeStr.substring(5);
					const end = item.recordEndTimeStr.substring(11);
					item._time = `${start}-${end}`;
					return item;
				});
				if (params.currentPage == 1) backData = [];
				backData = backData.concat(back);
				mySetState("recordData", backData);
				mySetState("total", res.data.total);
			}).catch(err=>{
			
			})
		},*/
	},
});

export default store;
store.dispatch("queryVideoConfig");

/**
 * 处理预告数据
 */
function handlerNoticeData(item) {
	item._beginTimeStr =
		item.beginTimeStr.substring(5, 11).replace("-", "月") + "日";
	item._timeQuantum =
		item.beginTimeStr.substring(11, 16) +
		" - " +
		item.endTimeStr.substring(11, 16);
	item._status = item.status === undefined ? "0" : item.status;
	return item;
}
